<template>
	<view class="classlist">
		<view class="loadingLayout" v-if="!classList.length && !nodata">
			<uni-load-more status="loading"></uni-load-more>
		</view>
		<view class="content">
			<navigator :url="'/pages/preview/preview?id='+item._id" class="item" v-for="item in classList" :key="item._id">
				<image :src="item.smallPicurl" mode="aspectFill"></image>
			</navigator>
		</view>
		<view class="loadingLayout" v-if=" classList.length || nodata">
			<uni-load-more :status="nodata?'noMore':'loading'"></uni-load-more>
		</view>
		<view class="safe-area-insert-bottom"></view>
	</view>
</template>

<script setup>
	import { ref } from 'vue';
	import {apiGetClassList,apiGetHistory} from '@/api/apis.js'
	import {onLoad,onUnload,onReachBottom,onShareAppMessage , onShareTimeline} from "@dcloudio/uni-app"
	import {gotoHome} from "@/utils/common.js"
	const classList = ref([])
	const nodata = ref(false)
	const query = {
		pageNum:1,
		pageSize:12
	}
	let pageName;
 	onLoad((e)=>{
		let {id=null,name=null,type=null} = e;
		// if(!id) gotoHome()
		if(type) query.type=type; 
		if(id) query.classid=id;
		pageName=name
		//修改导航标题
		uni.setNavigationBarTitle({
			title:pageName
		})
		getClassList();
	})
	
	onReachBottom(()=>{
		if (nodata.value) return;
		query.pageNum++;
		getClassList();
	})
	
	const getClassList = async ()=>{
		let res;
		if(query.classid){res = await apiGetClassList(query);}
		if(query.type){res = await apiGetHistory(query);}
		//触底加载更多 
		classList.value=[...classList.value ,...res.data.data];
		if(query.pageSize > res.data.data.length){
				nodata.value = true
		}
		uni.setStorageSync("storageClassList",classList.value)
	}
	//分享给好友
	onShareAppMessage((e)=>{
		return{
			title:"琨琨壁纸---"+pageName,
			//地址一定要带参数，是内页
			path:"/pages/classlist/classlist?id="+query.classid+"&name="+pageName
		}
	})
	//分享到朋友圈
	uni.showShareMenu({
		title:"琨琨壁纸---"+pageName,
		query:"id="+query.classid+"&name="+pageName
	})
	//离开页面
	onUnload(()=>{
		uni.removeStorageSync("storageClassList")
	})
</script>

<style lang="scss" scoped>
	.classlist{
		.content{
			display: grid;
			grid-template-columns: repeat(3,1fr);
			gap: 5rpx;
			padding: 5rpx;
			.item{
				height: 440rpx;
				image{
					width: 100%;
					height: 100%;
				}
			}
		}
	}
</style>
